<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+

class BaseModel extends BaseApp
{
	
	var $tbl_pref_custom;
	var $tbl_pref;	
	var $tables = array();
	var $custom_tables = array();
	var $db;
	var $limit;
	var $tbl;
	
	var $sql_params = '1';   					// will be added to select rows
	var $sql_params_ar = array();
	
	var $sql_params_select = '1';		
	var $sql_params_select_ar = array();
	
	var $sql_params_from = '';   			
	var $sql_params_from_ar = array();
	
	var $sql_params_order = ''; 
	
	
	
	function BaseModel() {

		$reg =& Registry::instance();
		$this->db 		=& $reg->getEntry('db');
		$this->tbl_pref =& $reg->getEntry('tbl_pref');
		$this->limit 	=& $reg->getEntry('limit');
		$this->extra 	=& $reg->getEntry('extra');
				  
		$this->tbl = $this->setTableNames();
	}
	
	
	function setTableNames() {
		$t = array();
		$t1 = $this->_setTableNames($this->tables, $this->tbl_pref, $this->tbl_pref_custom);
		$t2 = $this->_setTableNames($this->custom_tables, $this->tbl_pref);
		
		return (object) array_merge($t1, $t2);
	}
	
	
	function _setTableNames($tables, $tbl_pref = '', $tbl_pref_custom = '') {
		$tbl =  array(); 
		foreach($tables as $k => $v) {
			$t = (!is_int($k)) ? $k : $v;
			$tbl[$t] = $tbl_pref . $tbl_pref_custom . $v;
		}
		
		return $tbl;
	}
	
	
	// set diffrent sql params used when list records
	function setSqlParams($params, $key = null, $empty = false) {
		static $i = 0;
		$key = ($key) ? $key : $i++;
		if($empty) { $this->sql_params_ar = array(); }
		
		$this->sql_params_ar['default'] = 1;
		$this->sql_params_ar[$key] = $params;
		$this->sql_params = implode(' ', $this->sql_params_ar);
	}
	
	
	// set diffrent sql params used when list records
	function setSqlParamsSelect($params, $key = null) {
		static $i = 0;
		$key = ($key) ? $key : $i++;
		
		$this->sql_params_select_ar['default'] = 1;
		$this->sql_params_select_ar[$key] = $params;
		$this->sql_params_select = implode(', ', $this->sql_params_select_ar);
	}
	
	
	// set diffrent sql params used when list records
	function setSqlParamsFrom($params, $key = null) {
		static $i = 0;
		$key = ($key) ? $key : $i++;
		
		//$this->sql_params_from_ar['default'] = '';
		$this->sql_params_from_ar[$key] = $params;
		$this->sql_params_from = implode(', ', $this->sql_params_from_ar);
	}	
	
	
	function setSqlParamsOrder($val) {
		$this->sql_params_order = $val;
	}
	
	
		
	function getValuesArray($data, $id_field = 'id') {
		$ids = array();
		foreach($data as $k => $v) {
			$ids[] = $v[$id_field];
		}
		
		return $ids;
	}
	
	
	function getValuesString($data, $id_field = 'id') {
		return implode(',', $this->getValuesArray($data, $id_field));
	}

	
	function isExtra($module) {
		return (!empty($this->extra[$module]));
	}
}
?>
